package com.hui.admin.controller;

import com.hui.admin.common.result.Result;
import com.hui.admin.common.result.Results;
import com.hui.admin.service.FileService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * 文件控制层
 */
@RestController
@RequestMapping("/api/file")
public class FileController {

    @Resource
    private FileService fileService;

    /**
     * 文件下载
     */
    @GetMapping("/download/{fileName}")
    public void downloadFile(@PathVariable String fileName, HttpServletResponse response) throws IOException {
        fileService.downloadFile(fileName, response);
    }


    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) {
        return Results.success(fileService.uploadFile(file));
    }

    /**
     * 富文本编辑器（文件上传）
     */
    @PostMapping("/wang/upload")
    public Map<String, Object> wangEditorUpload(@RequestParam("file") MultipartFile file) {
        return fileService.wangEditorUpload(file);
    }
}
